﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.IO;
public partial class FileManager_Uploader : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        AuthenticateFileManager();
    }



    public void AuthenticateFileManager()
    {
        /* Edit this funcation to  AuthenticateFileManager
        string SessionID = Request["sessionid"].ToString();

        if (Request.Cookies[SessionID] != null)
        {

        }
        else
        {
            Response.Clear();
            Response.Write("Access Denied");
            Response.End();
        }*/

    }

    protected void bntUpload_Click(object sender, EventArgs e)
    {
        string FilePath = Server.MapPath("~")+txtPath.Text.Replace("//","/") + FileUpload1.FileName.ToString();
        FilePath = HttpUtility.UrlDecode(FilePath);
        if (System.IO.File.Exists(FilePath))
        {
            spanerror.InnerHtml = "<br/> " + FileUpload1.FileName.ToString() + " File Exists ";
        }
        else
        {
            try
            {
                FileUpload1.SaveAs(FilePath);
                string ext = Path.GetExtension(FileUpload1.FileName);
                if (ext.ToLower() == ".zip")
                {
                    Ionic.Zip.ZipFile zf = new Ionic.Zip.ZipFile(FilePath);
                    zf.ExtractAll(Server.MapPath("~") + txtPath.Text.Replace("//", "/"), Ionic.Zip.ExtractExistingFileAction.Throw);
                    
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.LastIndexOf("\\") >= 0)
                {
                    spanerror.InnerHtml = "<br/> " + ex.Message.Substring(ex.Message.LastIndexOf("\\"), ex.Message.Length - ex.Message.LastIndexOf("\\"));
                }else
                spanerror.InnerHtml = "<br/> " + ex.Message + "";
            }
        }
        
         
    }
}